/**
 * 消息内容表js
 */

$(function () {
    initialPage();
    getGrid();
});

function initialPage() {
    $(window).resize(function () {
        $('#dataGrid').bootstrapTable('resetView', {height: $(window).height() - 56});
    });
}

function getGrid() {
    $('#dataGrid').bootstrapTableEx({
        url: '../../rest/message/list?_' + $.now(),
        height: $(window).height() - 56,
        queryParams: function (params) {
            params.messageTitle = vm.keyword;
            params.status = vm.content;
            params.contentId = vm.type;
            return params;
        },
        columns: [
            {checkbox: true},
            {
                title: '序号', align: "center", width: "40px",
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
            {field: "contentName", title: "内容分类", width: "100px"},
            {field: "businessName", title: "业务类型", width: "100px"},
            {field: "messageTitle", title: "消息标题", width: "100px"},
            {field: "createTime", title: "编辑时间", width: "100px"},
            {
                field: "status", title: "状态", width: "100px", formatter: function (value) {
                    if (value == 1) {
                        return "停用"
                    } else
                        return "启用"
                }
            },
            {
                title: "操作", width: "200px", formatter: function (value, row, index) {
                    var _html = '';
                    _html += '<a href="javascript:;" onclick="vm.query(\'' + row.messageId + '\')" title="查看" style="padding: 0 10px"><span style="font-size: 14px">查看</span></a>';
                    _html += '<a href="javascript:;" onclick="vm.edit(\'' + row.messageId + '\')" title="编辑" style="padding: 0 10px"><span style="font-size: 14px">编辑</span></a>';
                    _html += '<a href="javascript:;" onclick="vm.remove(false,\'' + row.messageId + '\')"  style="padding: 0 10px"><span style="font-size: 14px">删除</span></a>';
                    _html += '<a href="javascript:;" onclick="vm.update(\'' + row.messageId + '\',\'' + row.status + '\')" title="启用/停用" style="padding: 0 10px"><span style="font-size: 14px">启用/停用</span></a>';
                    return _html;
                }
            }
        ]
    })
}

var vm = new Vue({
    el: '#dpLTE',
    data: {
        keyword: null,
        content: null,
        type: null,
    },
    methods: {
        load: function () {
            $('#dataGrid').bootstrapTable('refresh');
        },
        save: function () {
            toUrl("/base/message/add1.html")
        },
        edit: function (messageId) {
            toUrl("/base/message/edit1.html?messageId=" + messageId)
        },
        update: function (messageId,status) {

            if (status == 1){
                status = 0;
            }else
                status = 1;
            console.log(status)
            var message = {
                messageId : messageId,
                status : status
            };
            $.ajax({
                url: '../../rest/message/update',
                type: 'post',
                contentType: 'application/json',
                data: JSON.stringify(message),
                success: function (data) {
                    if (data.code === 0) {
                        vm.load();
                    } else {
                        alert(data.msg)
                    }
                },
            })
        },
        query: function (messageId) {
            toUrl("/base/message/query.html?messageId=" + messageId)
        },
        remove: function (batch, messageId) {
            var ids = [];
            if (batch) {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (!checkedArray(ck)) {
                    return false;
                }
                $.each(ck, function (idx, item) {
                    ids[idx] = item.messageId;
                });
            } else {
                ids.push(messageId);
            }
            $.RemoveForm({
                url: '../../rest/message/remove?_' + $.now(),
                param: ids,
                success: function (data) {
                    vm.load();
                }
            });
        },
        selectContent: function (e) {
            this.content = e.target.value;
        },
        selectType: function (e) {
            this.type = e.target.value;
        },
        reload: function () {
            this.keyword = null;
            this.content = null;
            this.type = null;
            vm.load();
        }
    }
})